package com.example.myanimator.mcanvas

import android.content.Context
import android.graphics.*
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.ArcShape
import android.graphics.drawable.shapes.OvalShape
import android.graphics.drawable.shapes.RectShape
import android.graphics.drawable.shapes.RoundRectShape
import android.util.AttributeSet
import android.view.View
import androidx.core.graphics.drawable.toBitmap
import com.example.myanimator.R

/**
 * @author tgw
 * @date 2021/4/2
 * @describe ShapeDrawable 画一个矩形  为什么 left =50  却感觉在中间因为xml中设置了  margin
 */
class ShapeDrawableView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {

    private val mPaint: Paint
    private var shapeDrawable: ShapeDrawable? = null
    private var mBmpCanvas: Canvas? = null //只能定义一个bitmap 负载到canvas上 在画出bitmap
    private var mBmp: Bitmap? = null
    override fun onDraw(canvas: Canvas?) {
//        super.onDraw(canvas)
//        mBmpCanvas = null
//        mBmpCanvas?.let { shapeDrawable?.draw(it) }
        //红色背景
        canvas?.drawBitmap(mBmp!!,0f,0f,mPaint)
        //黄色方块
        shapeDrawable?.draw(canvas!!)

        //椭圆
        shapeDrawable = ShapeDrawable(OvalShape())
        shapeDrawable?.let {
            it.bounds = Rect(50, 200, 200, 400)
            it.paint.color = Color.BLACK
            it.draw(canvas!!)
        }

        //扇形方块
        shapeDrawable = ShapeDrawable(ArcShape(20f,320f))
        shapeDrawable?.let {
            it.bounds = Rect(300, 200, 500, 600)
            it.paint.color = Color.BLACK
            it.draw(canvas!!)
        }

        //圆角矩形
        val outerR = floatArrayOf(12f,12f,0f,0f,12f,12f,0f,0f)//外矩形的各个角度大小  左上角开始顺时针
        val  rectF = RectF(60f,60f,60f,60f)//内矩形与外矩形的 边距
        val innerR = floatArrayOf(52f,12f,0f,0f,12f,50f,0f,0f)//内矩形的各个角度大小
        shapeDrawable = ShapeDrawable(RoundRectShape(outerR,rectF,innerR))
        shapeDrawable?.let {
            it.bounds = Rect(500, 20, 700, 200)
            it.paint.color = Color.YELLOW
            canvas?.let { it1 -> it.draw(it1) }
        }


        //Shader
        shapeDrawable = ShapeDrawable(RectShape())
        var bitmap = BitmapFactory.decodeResource(resources, R.drawable.dog)
        var bitmapShader = BitmapShader(bitmap,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP)
        shapeDrawable?.let {
            it.bounds = Rect(500, 400, 700, 200)
            it.paint.color = Color.BLACK
//            it.paint.shader =bitmapShader
            canvas?.let { it1 -> it.draw(it1) }
        }
    }

    init {
        setLayerType(LAYER_TYPE_SOFTWARE, null) //禁用加速
        mPaint = Paint()
        shapeDrawable = ShapeDrawable(RectShape())
        //因为定义
        shapeDrawable?.let {
            it.bounds = Rect(50, 50, 200, 200)
            it.paint.color = Color.YELLOW

            //创建一个canvas图层
            mBmp = it.toBitmap(1000, 1000, config = Bitmap.Config.ARGB_8888)
            mBmpCanvas= Canvas(mBmp!!)
            mBmpCanvas?.drawColor(Color.RED)
        }
    }

}